package com.hqyj.javaSpringBoot.modules.account.controller;

import com.github.pagehelper.PageInfo;
import com.hqyj.javaSpringBoot.modules.account.entity.Role;
import com.hqyj.javaSpringBoot.modules.account.service.RoleService;
import com.hqyj.javaSpringBoot.modules.common.entiy.ResultEntity;
import com.hqyj.javaSpringBoot.modules.common.entiy.SearchBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class RoleController {


    @Autowired
    private RoleService roleService;

    /**
     * 127.0.0.1/api/role
     * {"roleName":"admin"}
     * 添加角色，因为角色具有唯一性，所以先根据角色名字进行一次检索，
     * 如果存在就不在添加，不存在则添加
     * @param role 角色
     * @return 添加结果和对象
     */
    @PostMapping(value = ("/role"),consumes = "application/json")
    public ResultEntity<Role> insertRole(@RequestBody Role role){
        return roleService.insertRole(role);
    }

    /**
     * 127.0.0.1/api/role/{id}
     * @param id 角色id
     * @return 结果
     */
    @DeleteMapping(value = "/role/{id}")
    public ResultEntity<Role> deleteRole(@PathVariable int id){
        return roleService.deleteRole(id);
    }

    /**
     * 127.0.0.1/api/role
     * {"roleName":"admin1"}
     * 修改角色
     * @param role 角色
     * @return 添加结果和对象
     */
    @PutMapping(value = "/role",consumes = "application/json")
    public ResultEntity<Role> updateRole(@RequestBody Role role){
        return roleService.updateRole(role);
    }

    /**
     * 127.0.0.1/api/{id}
     * 根据角色id查询
     * @param id 角色id
     * @return 结果
     */
    @GetMapping(value = "/role/{id}")
    public Role getRoleByRoleId(@PathVariable int id){
        return roleService.getRoleByRoleId(id);
    }

    /**
     * 127.0.0.1/api/roles
     * 查询所有角色
     * @return 结果
     */
    @GetMapping(value = "/roles")
    public List<Role> getRoles(){
        return roleService.getRoles();
    }

    /**
     * 127.0.0.1/api/roles
     * {"currentPage":1, "pageSize":5, "orderBy":"id",
     *  "direction":"desc", "keyword": "增加"}
     * @param searchBean
     * @return 结果
     */
    @PostMapping(value = "/roles",consumes = "application/json")
    public PageInfo<Role> getRoleBySearch(@RequestBody SearchBean searchBean){
        return roleService.getRoleBySearch(searchBean);
    }
}
